package com.deryun.tyl.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.deryun.tyl.bean.Contact;
import com.deryun.tyl.bean.GroupMessage;
import com.deryun.tyl.bean.User;
import com.deryun.tyl.bean.tempBean.FriendConnect;
import com.deryun.tyl.dao.impl.GroupMessageDaoImpl;

@Controller
@RequestMapping("groupMessage")
public class GroupMessageController 
{

	@Resource(name="groupMessageDaoImpl")
	private GroupMessageDaoImpl dao;

	//首页友情链接
	@RequestMapping("/toFriendConnect")
	public String toFriendConnect(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) 
		{
			return "redirect:/civil/homepage";
		}
		List<GroupMessage> list = dao.getAllContact();
		request.setAttribute("massage", list);
		request.setAttribute("result", request.getAttribute("result"));
		return "back_admin_manger/edit_group_message";
	}
	
		//设置友情链接
		@RequestMapping("/friendConnect")
		public String friendConnect(HttpServletRequest request)
		{
			User user = (User) request.getSession().getAttribute("user");
			if (user == null) 
			{
				return "redirect:/civil/homepage";
			}
			try 
			{
				List<GroupMessage> list = dao.getAllContact();
				for(GroupMessage f : list)
				{
					String nameNum = "name"+f.getId(); 
					String positionNum = "position"+f.getId(); 
					if(null != request.getParameter(nameNum) && null != request.getParameter(positionNum))
					{	
						if(!f.getGroupName().trim().equals(request.getParameter(nameNum).trim()) || !f.getGroupHttp().trim().equals(request.getParameter(positionNum).trim()))
						{
							dao.modifyFC(request.getParameter(nameNum).trim(), request.getParameter(positionNum).trim(),String.valueOf(f.getId()));
						}
					}	
				}
				request.setAttribute("result", "success");
				return "forward:/groupMessage/toFriendConnect";
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
				request.setAttribute("result", "failed");
				return "forward:/groupMessage/toFriendConnect";
			}
		}
}
